/**
 * @author LKQ
 * @date 2022/2/28 14:02
 * @description 注意不能使用排序，只能用滑动窗口
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = {11, 13, 17, 23, 29, 31, 7, 5, 2, 3};
        System.out.println(solution.numOfSubarrays(arr, 3, 5));
    }

    public int numOfSubarrays(int[] arr, int k, int threshold) {
        int n = arr.length, cnt = 0;
        // 将除转变为目标整数
        int targetSum = k * threshold, sum = 0;
        for (int i = 0; i < n; i++) {
            sum += arr[i];
            if(i >= k - 1) {
                // 长度k的子数组和
                if (sum >= targetSum) {
                    cnt++;
                }
                // 先减去队首，下一次循环加上队尾元素，长度还是K
                sum = sum - arr[i - k + 1];
            }
        }
        return cnt;
    }
}
